package com.minotauro.bean.property;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;

@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes("*")
public class GetterSetterProcessor extends AbstractProcessor {

  private List<PropertyBean> propertyBeanList = new ArrayList<PropertyBean>();

  private Trees trees;

  // --------------------------------------------------------------------------------

  public List<PropertyBean> getPropertyBeanList() {
    return propertyBeanList;
  }

  // --------------------------------------------------------------------------------

  public GetterSetterProcessor() {
    // Empty
  }

  // --------------------------------------------------------------------------------

  @Override
  public void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);

    trees = Trees.instance(processingEnv);
  }

  // --------------------------------------------------------------------------------

  @Override
  public boolean process( //
      Set<? extends TypeElement> annotations, //
      RoundEnvironment roundEnvironment) {

    GetterSetterTreeVisitor visitor = new GetterSetterTreeVisitor();

    for (Element element : roundEnvironment.getRootElements()) {
      TreePath treePath = trees.getPath(element);
      visitor.scan(treePath, trees);
      propertyBeanList = visitor.getPropertyBeanList();
    }
    return true;
  }
}
